package com.wxq.shape;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.Layout.Alignment;

public class MapText extends MapString {
	StaticLayout layout;
	float layoutWidth;
	float drawWidth;
	TextPaint textPaint;
	
	public MapText(float xCoord, float yCoord, String text,float width) {
		super(xCoord, yCoord, text);
		layoutWidth = width;
		drawWidth = layoutWidth;
		
		textPaint = new TextPaint();
		textPaint.setColor(Color.GREEN);
		textPaint.setAntiAlias(true);
		textPaint.setStyle(Style.STROKE);
		textPaint.setTextSize(25);
	}

	public void setTextLayout(Paint paint) {
		this.textPaint = (TextPaint) paint;
		setTextLayout();
	}

	public void setTextLayout()
	{
		layout = new StaticLayout(
				new String(this.text), 
				textPaint, 
				(int) drawWidth,
				Alignment.ALIGN_CENTER,
				(float) 1.0,
				(float) 0.0, 
				true);
	}
	
	
	@Override
	public void zoom(Matrix scale) {
		float [] point = new float []{xCoord,yCoord,xCoord+layoutWidth,yCoord};
		scale.mapPoints(point);
		xDrawCord = point[0];
		yDrawCord = point[1];
		drawWidth = point[2]-point[0];	
		setTextLayout();
	}

	@Override
	public void draw(Canvas canvas, Paint paint) {
		if(layout==null)
		{
			setTextLayout(paint);
		}
		canvas.save();
		canvas.translate(xDrawCord+xOffset, yDrawCord+yOffset);
		layout.draw(canvas);
		canvas.restore();
	}

}
